#pragma once
#include <iostream>
#include <fstream>
#include <string>


class Util
{
public:
    static bool tools(std::string path, std::string& result)
    {
        //std::cout << "path: " << path << std::endl;
        //这里有可能读取文件也会返回true因为Linux一切皆文件
        // std::ifstream file(path);
        // //判断文件是否打开成功
        // if(!file.is_open())
        // {
        //     return false;
        // }
        // std::string line;
        // while(std::getline(file, line))
        // {
        //     //std::cout << "错误的俺照样读: " << line << std::endl;
        //     result += line;
        // }

        //要使用二进制的读取方法
        int filesize = FileSize(path);
        if(filesize > 0)
        {
            std::ifstream in(path);
            if(!in.is_open())
            {
                return false;
            }
            result.resize(filesize);
            in.read((char*)(result.c_str()),filesize);
            in.close();
        }
        else
        {
            return false;
        }                
        return true;
    }

    static bool ReadOneLine(std::string& str, std::string& out, const std::string& sep)
    {
        //std::cout << "ReadOneLine: " << str << std::endl;
        auto pos = str.find(sep);
        if(pos == std::string::npos)
        {
            std::cout << "ReadOneLine error " <<std::endl;
            return false;
        }
        out = str.substr(0,pos);
        
        str.erase(0, pos + sep.size());
        return true;
    }
    static int FileSize(const std::string& path)
    {
        std::ifstream in(path, std::ios::binary);//以二进制流的方式打开
        if(!in.is_open())
        {
            return -1;
        }
        
        in.seekg(0, in.end);
        int filesize = in.tellg();//获取当前位置
        //std::cout << "fileSize to this: " << filesize << std::endl;
        in.seekg(0,in.beg);
        in.close();
        return filesize;
    }
};